/**
 * @fileoverview Storage
 * @author 张松涛
 * @description localstorage封装
 */
const storage = {
  set(key: string, value: any) {
    // 环境判断，非浏览器环境不操作
    if (typeof window === 'undefined') return
    // 存储数据
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    } else {
      value = String(value)
    }
    localStorage.setItem(key, value)
  },
  get(key: string) {
    if (typeof window === 'undefined') return
    // 获取数据
    const data = localStorage.getItem(key)
    // 判断是否是对象压缩成的字符串
    if (data && data.includes('{')) {
      return JSON.parse(data)
    } else {
      return data
    }
  },
  remove(key: string) {
    if (typeof window === 'undefined') return
    localStorage.removeItem(key)
  },
  clear() {
    if (typeof window === 'undefined') return
    localStorage.clear()
  },
}

export default storage
